package com.blogSystem.type.python;

import org.python.util.InteractiveInterpreter;
import org.python.util.PythonInterpreter;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;

@Component
public class RunPython {
    public static String url="F:/blog_system/blog_system_article/src/main/java/com/blogSystem/article/predict.py";




    public static String executepy(String args)throws IOException, InterruptedException{

        System.out.println(args);
        try {
            //定义process对象  Define process object
            Process proc = Runtime.getRuntime().exec(args);
            //获取输出流  Get the output stream
            InputStreamReader isr = new InputStreamReader(proc.getInputStream(), "GBK");
            BufferedReader in = new BufferedReader(isr);
            //定义变量准备接收返回结果 Define variables ready to receive return results
            String line = null;
            System.out.println(in);
            line=in.readLine();
            return line;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }




    public String getType(String content) throws IOException, InterruptedException {
        try {
            /**
             * 定义脚本输入源数组:python3源,python脚本路径,python需要的变量(若干)
             *
             * Define the script input source array:
             * python3 source, python script path, python required variables (several)
             *
             */
            System.out.println(url);

            String args ="F:\\python\\python.exe "+url+" "+content;
            String[] args1 = new String[] { "python", url, content};
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date1 = df.format(System.currentTimeMillis());
            System.out.println(date1);

            /**
             * 把数组传给那个函数即可
             * Pass the array to that function.
             */

            String type=executepy(args);

            return type;

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return "";
    }
}
